package com.example.myapplication.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.example.myapplication.bean.TeacherInfo;

/**
 * Created by Administrator on 2016/4/14.
 */
public class PreferencesHelper {

    private static final String TEACHER_PREFERENCE = "teacherPreferences";
    private static final String TEACHER_ID = "teacher_id";
    private static final String TEACHER_NAME = "teacher_name";
    private static final String TEACHER_EMAIL = "teacher_email";
    private static final String TEACHER_PASSWORD = "teacher_password";
    private static final String TEACHER_PIC = "teacher_pic";
    private static final String TEACHER_NUMBER = "teacher_number";
    public PreferencesHelper(){

    }

    public static void writetoPreference(Context context, TeacherInfo teacherInfo){
        SharedPreferences.Editor editor = getEdit(context);
        editor.putString(TEACHER_ID,teacherInfo.getTeacher_id());
        editor.putString(TEACHER_EMAIL,teacherInfo.getTeacher_email());
        editor.putString(TEACHER_NAME,teacherInfo.getTeacher_name());
        editor.putString(TEACHER_PASSWORD,teacherInfo.getTeacher_password());
        editor.putString(TEACHER_PIC,teacherInfo.getTeacher_pic());
        editor.putString(TEACHER_NUMBER,teacherInfo.getTeacher_number());
        editor.apply();
      //  editor.commit();

    }

    public static TeacherInfo getTeacher(Context context){
        SharedPreferences sharedPreferences  = getSharedPreferences(context);
        final String teachername = sharedPreferences.getString(TEACHER_NAME,null);
        final String teacherid = sharedPreferences.getString(TEACHER_ID,null);
        final String teacherpassword = sharedPreferences.getString(TEACHER_PASSWORD,null);
        final String teacher_number = sharedPreferences.getString(TEACHER_NUMBER,null);
        final String teacher_email = sharedPreferences.getString(TEACHER_EMAIL,null);
        if(null==teacher_number||null==teacherid||null==teacherpassword||null==teachername||null==teacher_email){
            return null;
        }
        TeacherInfo teacherInfo = new TeacherInfo();
        teacherInfo.setTeacher_name(teachername);
        teacherInfo.setTeacher_password(teacherpassword);
        teacherInfo.setTeacher_number(teacher_number);
        teacherInfo.setTeacher_email(teacher_email);
        return teacherInfo;
    }

    public static void signOut(Context context) {
        SharedPreferences.Editor editor = getEdit(context);
        editor.remove(TEACHER_EMAIL);
        editor.remove(TEACHER_NUMBER);
        editor.remove(TEACHER_PASSWORD);
        editor.remove(TEACHER_NAME);
        editor.remove(TEACHER_PIC);
        editor.remove(TEACHER_ID);
        editor.apply();
    }

    public static boolean isSignedIn(Context context) {
        final SharedPreferences preferences = getSharedPreferences(context);
        return preferences.contains(TEACHER_EMAIL) &&
                preferences.contains(TEACHER_PASSWORD) &&
                preferences.contains(TEACHER_NUMBER)&&
                preferences.contains(TEACHER_NAME)&&
                preferences.contains(TEACHER_PIC)&&
                preferences.contains(TEACHER_PIC);
    }

    public static SharedPreferences.Editor getEdit(Context context){
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.edit();
    }

    public static SharedPreferences getSharedPreferences(Context context){
        return context.getSharedPreferences(TEACHER_PREFERENCE,Context.MODE_PRIVATE);
    }

}
